package com.lwb.ztx.user.widget;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lwb.ztx.R;
import com.lwb.ztx.game.Game2048Activity;
import com.lwb.ztx.user.widget.content.UserItem;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;


public class UserFragment extends Fragment implements UserItemAdapter.OnItemClickListener{

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private static final int TYPE_COLLENT = 1;
    private static final int TYPE_PICTURE = 2;
    private static final int TYPE_DOWNLOAD = 3;
    private static final int TYPE_SETTING = 4;
    private static final int TYPE_ABOUT = 5;
    private static final int TYPE_GAME = 6;

    private String mParam1;
    private String mParam2;
    @InjectView(R.id.user_items_recycler_view)
    RecyclerView recyclerView;
    UserItemAdapter adapter;
    private View rootView;

    public UserFragment() {
        // Required empty public constructor
    }

    public static UserFragment newInstance(String param1, String param2) {
        UserFragment fragment = new UserFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        if (rootView == null) {
            View view = inflater.inflate(R.layout.fragment_user, container, false);
            ButterKnife.inject(this, view);
            recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
            ArrayList<UserItem> items = new ArrayList<>();
            items.add(new UserItem(R.drawable.user_collect, "我的收藏",TYPE_COLLENT));
            items.add(new UserItem(R.drawable.user_game, "来盘游戏",TYPE_GAME));
//            items.add(new UserItem(R.drawable.user_download, "我的下载",TYPE_DOWNLOAD));
            items.add(new UserItem(R.drawable.user_settings, "设置",TYPE_SETTING));
            items.add(new UserItem(R.drawable.user_about, "关于",TYPE_ABOUT));
            adapter = new UserItemAdapter(items,this);
            recyclerView.setAdapter(adapter);
            rootView = view;
        }
        ViewGroup parentView = (ViewGroup) rootView.getParent();
        if (parentView != null) {
            parentView.removeView(rootView);
        }
        return rootView;

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onClick(UserItem item) {
        Intent intent;
        switch (item.getItemType()) {
            case TYPE_ABOUT:
                intent = new Intent(this.getActivity(),AboutActivity.class);
                this.getActivity().startActivity(intent);
                break;
            case TYPE_COLLENT:
                intent = new Intent(this.getActivity(),CollectActivity.class);
                this.getActivity().startActivity(intent);
                break;
            case TYPE_SETTING:
                intent = new Intent(this.getActivity(),SettingsActivity.class);
                this.getActivity().startActivity(intent);
                break;
            case TYPE_GAME:
                intent = new Intent(this.getActivity(),Game2048Activity.class);
                this.getActivity().startActivity(intent);
                break;
        }
    }
}
